package org.weasel.models;

import java.util.ArrayList;

import org.weasel.business.Company;
import org.weasel.business.Iteration;
import org.weasel.business.Project;
import org.weasel.business.Release;
import org.weasel.business.Unscheduled;
import org.weasel.business.UserStory;
import org.weasel.tree.TreeObject;
import org.weasel.tree.TreeObjectCompany;
import org.weasel.tree.TreeObjectIteration;
import org.weasel.tree.TreeObjectProject;
import org.weasel.tree.TreeObjectRelease;
import org.weasel.tree.TreeObjectUnscheduled;
import org.weasel.tree.TreeObjectUserStory;
import org.weasel.tree.TreeParent;

public class TreeModel {
	private Model model;
	private TreeParent invisibleRoot;
	
	public TreeModel(Model model) {
		this.model = model;
	}
	
	/**
	 * Removes a TreeObject from its parent
	 * @param treeObject - the TreeObject to remove
	 */
	public void removeTreeObject(TreeObject treeObject) {
		TreeParent to_parent = treeObject.getParent();
		to_parent.removeChild(treeObject);
	}
	
	public TreeParent loadTree() throws Exception {
		try {
			Company company = model.getCompany();
			TreeObjectCompany to_company = new TreeObjectCompany(company.getName(), company);
			
			for (Project project : company.getProjects()) {
				TreeObjectProject to_project = new TreeObjectProject(project.getName(), project);
				to_company.addChild(to_project);

				
				TreeParent to_releases = new TreeParent("Releases");
				to_project.addChild(to_releases);

				for (Release release : project.getReleases()) {
					TreeObjectRelease to_release = new TreeObjectRelease(release.getName(), release);
					to_releases.addChild(to_release);
					
					for (Iteration iteration : release.getIterations()) {
						TreeObjectIteration to_iteration = new TreeObjectIteration(iteration.getName(), iteration);
						to_release.addChild(to_iteration);
						
						for (UserStory userStory : iteration.getUserStories()) {
							TreeObjectUserStory to_userStory = new TreeObjectUserStory(userStory.getName(), userStory);
							to_iteration.addChild(to_userStory);
						}
					}

					Unscheduled releaseUnscheduled = release.getUnscheduled();
					TreeObjectUnscheduled to_releaseUnscheduled = new TreeObjectUnscheduled("Unscheduled", releaseUnscheduled);
					to_release.addChild(to_releaseUnscheduled);

					ArrayList<UserStory> unschUserStories = releaseUnscheduled.getUserStories();
					for (UserStory story : unschUserStories) {
						TreeObjectUserStory to_releaseUscheduledUserStory = new TreeObjectUserStory(story.getName(), story);
						to_releaseUnscheduled.addChild(to_releaseUscheduledUserStory);
					}
				}

				Unscheduled projectUnscheduled = project.getUnscheduled();
				TreeObjectUnscheduled to_projectUnscheduled = new TreeObjectUnscheduled("Unscheduled", projectUnscheduled);
				to_project.addChild(to_projectUnscheduled);
				ArrayList<UserStory> projectUnscheduledStories = projectUnscheduled.getUserStories();
				for (UserStory story : projectUnscheduledStories) {
					TreeObjectUserStory to_projectUscheduledUserStory = new TreeObjectUserStory(story.getName(), story);
					to_projectUnscheduled.addChild(to_projectUscheduledUserStory);
				}

			}
			invisibleRoot = new TreeParent("");
			invisibleRoot.addChild(to_company);

		} catch (Exception e) {
			// TODO Auto-generated catch block
			throw e;
		}
		return invisibleRoot;
	}
}
